package cn.cs.controller;

import cn.cs.common.Dto;
import cn.cs.common.DtoUtil;
import cn.cs.common.EmptyUtils;
import cn.cs.common.IdWorker;
import cn.cs.pojo.AccountDetail;
import cn.cs.pojo.UserBalance;
import cn.cs.service.UserBalanceService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/userBalance")
public class UserBalanceController {

    @Resource
    private UserBalanceService userBalanceService;


    /**
     * 分页查询 普通用户信息
     *
     * @param pageNumber
     * @param pageSize
     * @return
     * @throws Exception TODO 添加搜索
     */
    @RequestMapping(value = "/csPlatform/getUserBalanceByPage")
    public Dto getUserBalanceByPage(Integer pageNumber, Integer pageSize, String startDate, String endDate, String phone, String userId) throws Exception {
        Map<String, Object> param = new HashMap<>();
        if(EmptyUtils.isNotEmpty(startDate)){
            startDate = startDate + " 00:00:00";
        }
        if(EmptyUtils.isNotEmpty(endDate)){
            endDate = endDate + " 23:59:59";
        }
        param.put("startDate", startDate);
        param.put("endDate", endDate);
        param.put("phone", phone);
        param.put("userId", userId);
        return DtoUtil.returnSuccess("获取成功", userBalanceService.queryUserBalancePageByMap(param, pageNumber, pageSize));
    }

    /**
     * 根据userId查询用户余额信息 （废弃）
     * @param
     * @param
     * @return
     */
    @RequestMapping("/p/getUserBalance/{userId}")
    public Dto getUserBalance(@PathVariable("userId") String userId){
        try {
            UserBalance userBalance = userBalanceService.getUserBalanceById(userId);
            return DtoUtil.returnSuccess("查询余额成功", userBalance);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("请稍后重试","0003");
        }
    }

    /**
     * 根据用户id 查询账户明细
     * @param
     * @return
     */
    @RequestMapping("/wxApi/getAccountDetail")
    public Dto getAccountDetail(String userId){
        try {
            List<AccountDetail> accountDetailList = userBalanceService.getAccountDetail(userId);
            return DtoUtil.returnSuccess("查询成功", accountDetailList);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("请稍后重试","0003");
        }
    }

    /**
     * 待开发票的资金明细
     * @param
     * @return
     */
    @RequestMapping("/wxApi/getAccountDetailInvoice")
    public Dto getAccountDetailInvoice(String userId){
        try {
            List<AccountDetail> accountDetailList = userBalanceService.getAccountDetailInvoice(userId);
            return DtoUtil.returnSuccess("查询成功", accountDetailList);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("请稍后重试","0003");
        }
    }


    /**
     * 待开发票的资金明细 -- 电器厂pc
     * @param
     * @return
     */
    @RequestMapping("/dqcApi/getAccountInvoice")
    public Dto getAccountInvoice(String factoryId){
        try {
            List<AccountDetail> accountDetailList = userBalanceService.getAccountDetailInvoice(factoryId);
            return DtoUtil.returnSuccess("查询成功", accountDetailList);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("请稍后重试","0003");
        }
    }

    /**
     * 账户充值业务
     * @param
     * @return
     */
    @RequestMapping("/wxApi/addUserBalance")
    @Transactional
    public Dto addUserBalance(String userId, String amount){
        if(EmptyUtils.isEmpty(userId)){
            return DtoUtil.returnFail("用户不存在，请重新授权登陆", "userNull001");
        }
        String id = IdWorker.getId();
        AccountDetail accountDetail = new AccountDetail();
        try {

            accountDetail.setId(id);
            accountDetail.setTransactionTime(new Date());
            accountDetail.setTransactionType(1);
            accountDetail.setUserId(userId);
            accountDetail.setTitle("微信充值");
            accountDetail.setPayStatus("1");
            accountDetail.setAmount(Double.valueOf(amount));
            userBalanceService.addAccountDetail(accountDetail);

        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("请稍后重试","0003");
        }
        return DtoUtil.returnSuccess("操作成功", accountDetail);
    }

}
